var realTimeQueue:Array=new Array();
var noticeQueue:Array=new Array();

var currentStatus:String="idle";

// catalog 0 - Platform, 1 - Oper, 2 - User, 3 - Community
function send(msg:String, catalog:Number, type:Number)
{
	var nav_basic=Packages.system.mlf.GateKeeper.scanElement("NavigationBar", "basic");
	var content=Packages.system.mlf.GateKeeper.scanDictionary(nav_basic, "CATALOG_"+catalog)+": "+msg;
	pushMessage(Packages.system.utils.TextUtil.formatText("tfContent", content), type);
}

// content might be ml2 template string or htmlText
// type 0 - realtime, 1 - notice, etc - notice
function pushMessage(content:String, type:Number)
{
	if (type==0)
	{
		realTimeQueue.push(content);
	}
	else
	{
		noticeQueue.push(content);
	}

	if (currentStatus=="idle")
	{
		// Packages.log("pushMessage.idle status", "debug");
		
		launchProcess(true);
		currentStatus="process";
	}
}

function launchProcess(firstFlag:Boolean)
{
	// Packages.log("launchProcess."+firstFlag, "debug");
	
	if (realTimeQueue.length>0)
	{
		if (firstFlag) 
		{
			showMessage("@start");
			startInterval();
		}
		else showMessage(realTimeQueue.shift());
	}
	else if (noticeQueue.length>0)
	{
		if (firstFlag) 
		{
			showMessage("@start");
			startInterval();
		}
		else showMessage(noticeQueue.shift());
	}
	else 
	{
		showMessage("@end");
		closeInterval();
	}
}

function showMessage(content)
{
	// Packages.log("showMessage."+content, "debug");
	
	if (content=="@start") Packages.components.basic.NavigationBar.messageDisplay(-80);
	else if (content=="@end") Packages.components.basic.NavigationBar.messageDisplay(80);
	
	// refresh message transition
	
	var contentField=Packages.system.mlf.GateKeeper.scanElement("NavigationBar", "content");
	var nav_basic=Packages.system.mlf.GateKeeper.scanElement("NavigationBar", "basic");
	content=Packages.system.mlf.GateKeeper.scanDictionary(nav_basic, content);
	contentField.htmlText=content;
}

function startInterval()
{
	// Packages.log("startInterval.", "debug");
	
	Packages.system.mlf.GateKeeper.addListener("onMessageIntervalEvent", this);
	
	var noticeEvent:Object=new Object();
	noticeEvent.type="onMessageIntervalEvent";
	noticeEvent.gap=5;
	noticeEvent.count=0;
	Packages.system.display.Timer.regNoticeEvent(noticeEvent);
}

function closeInterval()
{
	// Packages.log("closeInterval.", "debug");
	
	Packages.system.mlf.GateKeeper.removeListener("onMessageIntervalEvent", this);
	Packages.system.display.Timer.removeNoticeEvent("onMessageIntervalEvent");
	currentStatus="idle";
}

function onMessageIntervalEvent(evt:Object)
{
	// Packages.log("onMessageIntervalEvent.", "debug");
	
	launchProcess();
}